const app = getApp();
Page({
	onLoad: function(options) {
		this.options = options;
		// console.log(options,'传送')
		var sessionId = wx.getStorageSync("sessionid");
		var openid = wx.getStorageSync("openid");
		var icon = wx.getStorageSync('icon');
		var nickname = wx.getStorageSync('nickname');
		var user_id = wx.getStorageSync('user_id');
		//步数授权
		wx.getWeRunData({
			fail() {
				console.log('步数授权');
			},
			success(res) {}
		});	
		app.loginStep(this)  //重新登录
		
// 		//查询是否授权
// 		if(!sessionId || !openid || !icon || !nickname || !user_id){
// 			if (typeof that.options.jump === 'undefined' || that.options.jump == 'undefined' || !that.options.jump) {
// 				wx.redirectTo({
// 					url: '../index/index'
// 				});
// 			} else {
// 				wx.redirectTo({
// 					url: that.options.jump + '?goods_id=' + that.options.goods_id + '&type=' + that.options.type + '&id=' + that.options.id +
// 						'&user_id=' + that.options.u_id + '&activity_id=' + that.options.a_id,
// 				});
// 			};
// 		}
		
		
	},
	getUserInfo: function() {
		var that = this;
		wx.showLoading({
			title: '信息授权中',
		});
		wx.getUserInfo({
			lang: "zh_CN",
      fail() {
        wx.hideLoading()
      },
			success: res => {
				var userInfo = res.userInfo;
				var openid = wx.getStorageSync("openid");
				// app.setWxUserInfo(userInfo);
				var params = {
					nickname: userInfo.nickName,
					gender: userInfo.gender,
					icon: userInfo.avatarUrl,
					invite_user: wx.getStorageSync('invite_user_user_id'),
					zone_name: userInfo.province + ' ' + userInfo.city,
				};
				app.request(app.apiDomain + 'index.php?c=user&a=profile', params, function(res) {
					//把数据写入Data
					params.user_id = res.data.data.id;
					// console.log(res)
					app.setApiUserInfo(params);
					//app.loginStep(that)  重新登录
					if (res.data.status == 1) {
						wx.hideLoading();
						if (typeof that.options.jump === 'undefined' || that.options.jump == 'undefined' || !that.options.jump) {
							wx.redirectTo({
								url: '../index/index' + '?new_url=' + that.options.new_url
							});
						} else {
							wx.redirectTo({ 
								url: that.options.jump + '?goods_id=' + that.options.goods_id + '&type=' + that.options.type + '&id=' + that.options.id +
									'&user_id=' + that.options.user_id + '&activity_id=' + that.options.activity_id + '&new_url=' + that.options.new_url,
							});
						};
					};
				});
			},
		})
	},
})
